package m.jclecai.com.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.AppUtils;
import m.jclecai.com.utils.MyLog;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.CircularProgressBar;

/**
 * Created by ZD on 2017/7/5.
 */

public class SplashActivity extends BaseActivity {

    @BindView(R.id.tv_continue)
    TextView tvContinue;
    @BindView(R.id.cb_progress)
    CircularProgressBar cbProgress;
    @BindView(R.id.li_bg)
    LinearLayout li_bg;
    CountDownTimer timer;
    private int status = 1;

    @Override
    protected int getStatusBarColor() {
        return R.color.color_a3e9fd;
    }

    @Override
    protected int getContentView() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        super.initView();
        if (getPackageName().equals("m.jclecai.com")) {
            //乐彩
            li_bg.setBackgroundResource(R.drawable.spalsh);
        }else if(getPackageName().equals("m.jczhongcai.com")){
            //中彩
            li_bg.setBackgroundResource(R.drawable.spalsh_zhongcai);
        }
//        sendReqVersionControl();
        countDowm(3000);
       /* if(!PreferenceUtil.isShowGuideFirst(getBaseContext())){

        }else {
            startActivity(new Intent(getBaseContext(),GuideActivity.class));
            finish();
        }*/
    }


    private String url = "";
    /**
     *  版本控制接口
     */
    private void sendReqVersionControl() {
        Constant_Url.HTTP_URL = MyApplication.HTTP_URL_SWITCH;
        url = Constant_Url.VERSION_CONTROL+ AppUtils.getVersionCode(SplashActivity.this);
//        url = Constant_Url.VERSION_CONTROL;
        HttpManager.getAsyn(url, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyLog.i(request.toString());
            }

            @Override
            public void onResponse(String response) {
                Constant_Url.HTTP_URL = MyApplication.HTTP_URL;
                try {
                    if(response != null && response.startsWith("\ufeff"))
                    {
                        response =  response.substring(1);
                    }
                    JSONArray jsonArray = new JSONArray(response);

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        if (jsonObject != null) {
                            //status=0  表示开启
                            //status=1  表示关闭
                            status = jsonObject.optInt("status");
                            MyApplication.status = status;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        });



    }




    /**
     * @param time     倒计时的时间
     */
    public void countDowm(final long time) {
        timer = new CountDownTimer(time, 1) {
            @SuppressWarnings("deprecation")
            @SuppressLint("NewApi")
            @Override
            public void onTick(long millisUntilFinished) {
                if (isFinishing()) return;
                cbProgress.setProgress((time - millisUntilFinished) / 1f / time * 100);
                tvContinue.setText(((millisUntilFinished / 1000) + 1)+"\n跳过");
            }

            @SuppressWarnings("deprecation")
            @SuppressLint("NewApi")
            @Override
            public void onFinish() {
                if (isFinishing()) return;
                cbProgress.setProgress(100);
                startActivity(new Intent(getBaseContext(), MainActivity.class));
//                if (status==1){
//                    startActivity(new Intent(getBaseContext(), MainSwitchActivity.class));
//                }else {
//                    startActivity(new Intent(getBaseContext(), MainActivity.class));
//                }
                finish();
//                cbProgress.setProgress(100);
//                tvContinue.setText("跳过");
//                tvContinue.setEnabled(true);
            }
        };
        timer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timer != null)
            timer.cancel();
    }

    @OnClick(R.id.mRlJump)
    void jump() {
        startActivity(new Intent(getBaseContext(), MainActivity.class));
//        if (status==1){
//            startActivity(new Intent(getBaseContext(), MainSwitchActivity.class));
//        }else{
//            startActivity(new Intent(getBaseContext(), MainActivity.class));
//        }
        finish();
    }
}
